## This software is licensed by the MIT License, see LICENSE file ##
##                                    Copyright © 2022 whitemangl ##

import sys
import os

from typing import Union
from typing import Dict

import json

__DEFAULT_GENERAL_CONF_PATH__ = os.path.join(os.path.dirname(sys.argv[0]), "config.json")
__DEFAULT_PRIVATE_CONF_PATH__ = os.path.join(os.path.dirname(sys.argv[0]), "private.json")

class Configuration():

    def __getattr__(self, attr: str) -> Union[str, None]:
        ret = self.__conf_obj__.get(attr.lower(), "")
        return ret if ret else None

    def __dict__(self) -> Dict[str, str]:
        return self.__conf_obj__

    def __init__(self, config_filename: str = __DEFAULT_GENERAL_CONF_PATH__):

        if config_filename is __DEFAULT_GENERAL_CONF_PATH__ and os.path.exists(__DEFAULT_PRIVATE_CONF_PATH__):
            config_filename = __DEFAULT_PRIVATE_CONF_PATH__

        self.__conf_obj__: Dict[str, str] = dict()

        if os.path.exists(config_filename):
            with open(config_filename, "r", encoding="utf8") as fd:
                ret = json.load(fd)
            for k, v in ret.items():
                self.__conf_obj__.update([(str(k).lower(), str(v))])
